home *** CD-ROM | disk | FTP | other *** search
- Path: CS.Arizona.EDU!not-for-mail
- From: kdb@CS.Arizona.EDU (Koen De Bosschere)
- Newsgroups: comp.lang.c++
- Subject: C++ meta-problem
- Date: 31 Mar 1996 14:32:32 -0700
- Organization: University of Arizona CS Department, Tucson AZ
- Message-ID: <4jmtlg$smm@baskerville.CS.Arizona.EDU>
- NNTP-Posting-Host: baskerville.cs.arizona.edu
-
- In order to prevent to write an iterator for every operation
- I want to perform on a list, I tried to write a kind of
- meta-iterator that would apply a particular method to
- all listnodes. For some reason, I cannot execute a
- function variable on an object. Does anyone has an
- idea how I can solve this problem?
-
- Thanks.
-
- -- Koen De Bosschere
-
- Test program:
-
- #include <iostream.h>
-
- class testclass {
- public:
- testclass(long d) { data = d; }
- ~testclass() {}
- void print() { cout << data << endl; }
- testclass *getnext() { return next; }
- private:
- long data;
- testclass *next;
- };
-
- // The next function applies the function "f" to all
- // the nodes starting at listnode "root".
-
- void iterate(testclass *root, void (testclass::*f)())
- {
- testclass *p = root;
- while (p) {
- p->f(); // <--- here the compiler generates an error (see below)
- p = p->getnext();
- }
- }
-
- main()
- {
- testclass *root;
- // ....
- iterate(root, testclass::print);
- // ....
- }
-
-
- Compiler (g++) error:
-
- testclass.cc: In function `void iterate(class testclass *, void (testclass::*)())':
- testclass.cc:18: no member function `testclass::f()' defined
-